import Vue from 'vue';
import VueRouter from 'vue-router';

import store from '@/store'

// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}



Vue.use(VueRouter)
// import Home from '@/pages/Home'
// import Search from '@/pages/Search'
// import Login from '@/pages/Login'
// import Register from '@/pages/Register'
// import Detail from '@/pages/Detail'
// import AddCartSuccess from '@/pages/AddCartSuccess'
// import ShopCart from '@/pages/ShopCart'
// import Trade from '@/pages/Trade'
// import Pay from '@/pages/Pay'
// import PaySuccess from '@/pages/PaySuccess'
import Center from '@/pages/Center'

// 引入二级路由组件
import MyOrder from '@/pages/Center/myOrder'
import GroupOrder from '@/pages/Center/groupOrder'
const router = new VueRouter({
  routes: [
    {
      path: '/',
      redirect: '/home'
    },
    {
      path: '/center',
      component: Center,
      meta: { isShow: true },
      // 二级路由组件
      children: [
        {
          path: 'myorder',
          component: MyOrder
        },
        {
          path: 'grouporder',
          component: GroupOrder
        },
        {
          path: '/center',
          redirect: '/center/myorder'
        }
      ]
    },
    {
      path: '/trade',
      component: () => import('@/pages/Trade'),
      meta: { isShow: true },
      // 路由独享守卫
      beforeEnter: (to, from, next) => {
        // 去交易页面，必须是从购物车而来
        if (from.path == '/shopcart') {
          next()
        } else {
          // 其他的路由组件来的，就停留在当前
          next(false)
        }
      }
    },
    {
      path: '/pay',
      component: () => import('@/pages/Pay'),
      meta: { isShow: true },
      beforeEnter: (to, from, next) => {
        if (from.path == '/trade') {
          next()
        } else {
          next(false)
        }
      }
    },
    {
      path: '/paysuccess',
      component: () => import('@/pages/PaySuccess'),
      meta: { isShow: true }
    },
    {
      path: '/shopcart',
      component: () => import('@/pages/ShopCart'),
      name: 'shopcart',
      meta: { isShow: true }
    },
    {
      path: '/addcartsuccess',
      component: () => import('@/pages/AddCartSuccess'),
      name: 'addcartsuccess',
      meta: { isShow: true }
    },
    {
      path: '/home',
      component: () => import('@/pages/Home'),
      // meta 表示 路由元信息
      meta: { show: true }
    },
    {
      path: '/Search',
      component: () => import('@/pages/Search'),
      meta: { show: true },
      name: 'search'
    },
    {
      path: '/Login',
      component: () => import('@/pages/Login'),
      meta: { show: false }
    },
    {
      path: '/Register',
      component: () => import('@/pages/Register'),
      meta: { show: false }
    },
    {
      path: '/detail/:skuid?',
      component: () => import('@/pages/Detail'),
      meta: { show: false }
    },
  ],
  scrollBehavior(to, from, savedPosition) {
    return { y: 0 }
  },

})
// 全局守卫，前置守卫(在路由跳转之前进行跳转)
router.beforeEach(async (to, from, next) => {
  // next()
  // 用户登录了，才有token，未登录一定不会有token
  let token = store.state.user.token
  let name = store.state.user.userInfo.name
  if (token) {
    // 用户已经登录了还想去login  直接跳转到首页
    if (to.path == '/login' || to.path == '/register') {
      next('/')
    } else {
      // 登录了，但是去的不是login
      // 如果用户名已有
      if (name) {
        next()
      } else {
        // 没有用户信息，派发action 让仓库存储用户信息在跳转
        try {
          // 获取用户信息在首页展示
          await store.dispatch("getUserInfo");
          next()
        } catch (error) {
          // token失效了获取不到用户信息，从新登陆了
          // 清除token
          await store.dispatch('userLogout')
          next('/login')
        }
      }
    }
  } else {
    // 未登录 ,不能去交易相关、不能去支付相关】、不能去个人中心
    // 未登录去上面这些路由 -- 登录
    let toPath = to.path
    if (toPath.indexOf('/trade') != -1 || toPath.indexOf('/pay') != -1 || toPath.indexOf('/center') != -1) {
      // 把未登录的时候想去而没有去成的信息，存储与地址栏中
      next('/login?redirect=' + toPath)
    } else {
      // 去的不是要上面的这些路由

      next()

    }
  }
})
export default router

